package com.qp.basics.admin.controller;


import com.qp.basics.admin.service.DeptService;
import com.qp.basics.common.domain.UserInfo;
import com.qp.basics.common.repository.BaseRepository;
import com.qp.basics.common.service.dto.DeptDto;
import com.qp.basics.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Api(tags = "部门管理")
@RestController
@RequestMapping("/client/dept")
public class DeptClientController extends BaseController{

    @Resource
    private DeptService deptService;
    private static final String ENTITY_NAME = "dept";

    @Autowired
    private BaseRepository<UserInfo, Long> userInfoRepository;


    @Log("查询部门")
    @ApiOperation("查询部门:根据ID获取同级与上级数据")
    @GetMapping("/superior")
    public ResponseEntity<Object> getSuperior() {
        UserInfo me = getCurrentUserInfox();
        if(me == null){
            return failure("未登录");
        }
        me = userInfoRepository.get(me.getId(),UserInfo.class);
        if(me == null){
            return failure("未登录");
        }
            DeptDto deptDto = deptService.findById(me.getDept().getId());
            List<DeptDto> depts = deptService.getSuperior(deptDto, new ArrayList<>());
             return new ResponseEntity<>(depts, HttpStatus.OK);
    }

}
